<?php
class ModelCatalogReview extends Model {
	public function addReview($product_id, $data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "review SET author = '" . $this->db->escape($data['name']) . "', customer_id = '" . (int)$this->customer->getId() . "', product_id = '" . (int)$product_id . "', text = '" . $this->db->escape($data['text']) . "', rating = '" . (int)$data['rating'] . "', date_added = NOW()");

		$review_id = $this->db->getLastId();

		if (in_array('review', (array)$this->config->get('config_mail_alert'))) {
			$this->load->language('mail/review');
			$this->load->model('catalog/product');
			
			$product_info = $this->model_catalog_product->getProduct($product_id);

			$subject = sprintf($this->language->get('text_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));

			$message  = $this->language->get('text_waiting') . "\n";
			$message .= sprintf($this->language->get('text_product'), html_entity_decode($product_info['name'], ENT_QUOTES, 'UTF-8')) . "\n";
			$message .= sprintf($this->language->get('text_reviewer'), html_entity_decode($data['name'], ENT_QUOTES, 'UTF-8')) . "\n";
			$message .= sprintf($this->language->get('text_rating'), $data['rating']) . "\n";
			$message .= $this->language->get('text_review') . "\n";
			$message .= html_entity_decode($data['text'], ENT_QUOTES, 'UTF-8') . "\n\n";

			$mail = new Mail($this->config->get('config_mail_engine'));
			$mail->parameter = $this->config->get('config_mail_parameter');
			$mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
			$mail->smtp_username = $this->config->get('config_mail_smtp_username');
			$mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
			$mail->smtp_port = $this->config->get('config_mail_smtp_port');
			$mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');

			$mail->setTo($this->config->get('config_email'));
			$mail->setFrom($this->config->get('config_email'));
			$mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
			$mail->setSubject($subject);
			$mail->setText($message);
			$mail->send();

			// Send to additional alert emails
			$emails = explode(',', $this->config->get('config_mail_alert_email'));

			foreach ($emails as $email) {
				if ($email && filter_var($email, FILTER_VALIDATE_EMAIL)) {
					$mail->setTo($email);
					$mail->send();
				}
			}
		}
	}

	public function getReviewsByProductId($product_id, $start = 0, $limit = 20) {
	    $reviews = array();

		if ($start < 0) {
			$start = 0;
		}

		if ($limit < 1) {
			$limit = 20;
		}

		$reviews_main = $this->db->query("select pc.*, c.avatar from " . DB_PREFIX . "product_comments pc left join " . DB_PREFIX . "customer c on c.customer_id = pc.customer_id where pc.status = 1 and pc.product_id=" . (int)$product_id . " order by date_modified desc limit {$start}, {$limit}")->rows;
		if ($reviews_main) {
		    foreach ($reviews_main as $review) {
                $id = $review['id'];
                $images = $this->db->query("select image from " . DB_PREFIX . "product_comments_image where comments_id=" . (int)$id)->rows;
                $review_images = array();
                if ($images) {
                    foreach ($images as $image) {
                        $review_images[] = $image['image'];
                    }
                }
                $reviews[] = array(
                    'customer_name' => $review['customer_name'],
                    'avatar' => $review['avatar'],
                    'product_name' => $review['product_name'],
                    'product_model' => $review['product_model'],
                    'rating' => $review['rating'],
                    'comments' => $review['comments'],
                    'date_created' => $review['date_created'],
                    'date_modified' => $review['date_modified'],
                    'images' => $review_images
                );
            }
        }

        return $reviews;

	}

	public function getTotalReviewsByProductId($product_id) {
        $query = $this->db->query("select count(*) total from " . DB_PREFIX . "product_comments where status = 1 and product_id=" . (int)$product_id)->row;
		return $query ? $query['total'] : 0;
	}
}